﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SaveTable
{
    class Program
    {
        
        static void SaveRelativePro(int proId1,int proId2)
         {
             TheGioiPhaiDep_DataEntities db = new TheGioiPhaiDep_DataEntities();
             RelatedProduct re= new RelatedProduct();
             re.ProductId1 = proId1;
             re.ProductId2 = proId2;
             re.DisplayOrder = 0;
             db.RelatedProducts.Add(re);
             db.SaveChanges();
         }
        static void Main(string[] args)
        {
            TheGioiPhaiDep_DataEntities db = new TheGioiPhaiDep_DataEntities();
            List<int> listId =  new List<int>();
            List<Product_Category_Mapping> list = new List<Product_Category_Mapping>();
            list =  db.Product_Category_Mapping.ToList();
            Random r = new Random();
            Random r1 = new Random();
            Random r2 = new Random();
            Random r3 = new Random();
            Random r4 = new Random();
            
            for (int i = 0; i < list.Count(); i++)
            {
                
               // var value = list[r.Next(list.Count)];
                if (list.Count() > 4)
                {
                    int x = 0, y = 0, z = 0, w = 0;
                    for (int j = 0; j < list.Count(); j++)
                    {
                        var value1 = list[r1.Next(list.Count)];
                        if (list[i].CategoryId == value1.CategoryId && list[i].ProductId != value1.ProductId
                            &&
                           value1.ProductId != w && value1.ProductId != y
                            && value1.ProductId != z)
                        {
                            x = value1.ProductId;
                            break;
                        }
                    }
                    for (int h = 0; h < list.Count(); h++)
                    {
                        var value1 = list[r2.Next(list.Count)];
                        if (list[i].CategoryId == value1.CategoryId && list[i].ProductId != value1.ProductId && value1.ProductId != x
                            &&
                           value1.ProductId != w && value1.ProductId != z
                            && value1.ProductId != x)
                        {
                            y = value1.ProductId;
                            break;
                        }
                    }
                    for (int k = 0; k < list.Count(); k++)
                    {
                        var value1 = list[r3.Next(list.Count)];
                        if (list[i].CategoryId == value1.CategoryId && list[i].ProductId != value1.ProductId &&
                           value1.ProductId!= w && value1.ProductId != y 
                            && value1.ProductId  !=x)
                        {
                            z = value1.ProductId;
                            break;
                        }
                    }
                    for (int k = 0; k < list.Count(); k++)
                    {
                        var value1 = list[r4.Next(list.Count)];
                        if (list[i].CategoryId == value1.CategoryId && list[i].ProductId != value1.ProductId &&
                            value1.ProductId!= z && value1.ProductId != y 
                            && value1.ProductId  !=x )
                        {
                            w = value1.ProductId;
                            break;
                        }
                    }
                    Console.WriteLine("\nSave" + list[i].Id + " relatived with: " + x + "and" + y + " and  " + z +"and"+w+ "\n");
                    SaveRelativePro(list[i].Id, x);
                    SaveRelativePro(list[i].Id, y);
                    SaveRelativePro(list[i].Id, z);
                    SaveRelativePro(list[i].Id, w);
                }
                else if (list.Count() != 0)
                {
                    // Console.WriteLine("\nSave" + list[i].Id + " relatived with: " + x + "and" + y + " and  " + z + "\n");
                    SaveRelativePro(list[0].Id, list[i].Id);
                }
                else
                {
                    break;
                }
                

                //SaveRelativePro(list[i].Id, x);
                //SaveRelativePro(list[i].Id, y);
                //SaveRelativePro(list[i].Id, z);
                //SaveRelativePro(list[i].Id, w);
                //for (int j = 0; j < list.Count(); j++)
                //{

                //    var value1 = list[r1.Next(list.Count)];
                //    if (value != value1 && value.CategoryId== value1.CategoryId)
                //    {
                //        for (int h = 0; h < list.Count(); h++)
                //        {
                //            var value2 = list[r2.Next(list.Count)];
                //            if (value2 != value1)
                //            {
                //                for (int k = 0; k < list.Count(); k++)
                //                {

                //                    var value3 = list[r3.Next(list.Count)];
                //                    if (value3 != value2 && value3 != value1 && value3 != value)
                //                    {
                //                        Console.WriteLine(list[i].Id + " relatived with: " + value.Id + "and" + value1.Id + " and  " + value2.Id + "and " + value3.Id + "\n");
                //                        break;
                                       
                //                        //SaveRelativePro(list[i].Id, value.Id);
                //                        //SaveRelativePro(list[i].Id, value1.Id);
                //                        //SaveRelativePro(list[i].Id, value2.Id);
                //                        //SaveRelativePro(list[i].Id, value3.Id);
                                        
                //                    }
                //                    break;

                //                }
                //                break;
                //            }

                //            break;
                //        }
                      
                        //if (i + 1 == list.Count())
                        //{
                            
                        //    Console.WriteLine(list[i].Id + " relatived with: " + value.Id + "and" + value1.Id + " and  " + list[0].Id + "\n");
                          

                        //}
                        //else 
                        //Console.WriteLine(list[i].Id + " relatived with: " + value.Id + "and" + value1.Id + " and  " + list[i + 1].Id + "\n");

                     
                           
              
                //int index = Random.Next(10);
              
            }
            Console.WriteLine("\nCo: " + list.Count() + "\n");
            Console.ReadLine();

        }
    }
}
